﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Bovril.Rodney.Core
{
    internal class ServiceClientConfig
    {
        internal static ServiceClientConfig Load()
        {
            String localConfigDirectory = Bovril.Core.Config.GetLocalConfigDirectory();
            String serviceConfigPathname = Path.Combine(localConfigDirectory, "Bovril.Rodney.Core.ServiceClient.xml");

            if (!File.Exists(serviceConfigPathname))
                throw new ConfigLoadException(String.Format("Config file does not exist at '{0}'", serviceConfigPathname));

            try
            {
                XDocument serviceConfigDocument = XDocument.Load(serviceConfigPathname);

                return new ServiceClientConfig(serviceConfigDocument.Root);
            }
            catch (Exception ex)
            {
                throw new ConfigLoadException(String.Format("Unable to load config file '{0}'.  See inner exception for further details.", serviceConfigPathname), ex);
            }
        }

        internal ServiceClientConfig(XElement rootElement)
        {
            Username = rootElement.Element("Username").Value;
            Password = rootElement.Element("Password").Value;
        }

        internal String Username { get; private set; }
        internal String Password { get; private set; }
    }
}
